package com.lysj.bhp.borrowing.controller;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.borrowing.vo.CreditFileDetailVo;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/17--11:46
 * @Description: 借贷申请记录管理控制类
 */
@RestController
@RequestMapping("admin/borrowing_record")
public class BorrowingRecordController extends BorrowingContent {

    @RequestMapping("/list")
    public Resp list() {
        List<BorrowingRecord> borrowingRecordList = borrowingRecordRepository.findByStatusNotInOrderByApplyDateDesc(1);
        return new Resp(borrowingRecordList);
    }

    @RequestMapping("/credit_file_detail")
    public Resp creditFileDetail(Long userId,Integer borrowingType, Integer repayStatus) {
        List<CreditFileDetailVo> creditFileDetailVoList = new LinkedList<>();
        //查询指定还款状态的记录
        List<BorrowingRecord> borrowingRecordList = borrowingRecordRepository.findByUserIdAndBorrowingTypeAndRepayStatusOrderByApplyDateDesc(userId, borrowingType,repayStatus);
        for (BorrowingRecord borrowingRecord : borrowingRecordList) {
            CreditFileDetailVo creditFileDetailVo = new CreditFileDetailVo();
            creditFileDetailVo.setBorrowingOrderNo(borrowingRecord.getBorrowingOrderNo());
            ////查询指定还款状态的记录对应的还款期数
            creditFileDetailVo.setNper(borrowingInstallmentRepository.countByUserIdAndBorrowingRecordIdAndRepayStatus(userId, borrowingRecord.getBorrowingRecordId(), repayStatus));
            creditFileDetailVoList.add(creditFileDetailVo);
        }
        return new Resp(creditFileDetailVoList);
    }

}
